package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"

	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
)

type PlatformController struct{}

// List 场馆列表
func (that *PlatformController) List(ctx *fasthttp.RequestCtx) {

	id := string(ctx.QueryArgs().Peek("id"))
	state := ctx.QueryArgs().GetUintOrZero("state")
	maintained := ctx.QueryArgs().GetUintOrZero("maintained")
	gameType := ctx.QueryArgs().GetUintOrZero("game_type")
	page := ctx.QueryArgs().GetUintOrZero("page")
	pageSize := ctx.QueryArgs().GetUintOrZero("page_size")
	platform_type := ctx.QueryArgs().GetUintOrZero("platform_type")
	if page < 1 {
		page = 1
	}
	if pageSize < 10 || pageSize > 100 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	ex := g.Ex{}
	if id != "" {
		ex["id"] = id
	} else {
		if state > 0 {
			if state > 2 {
				helper.RetData(ctx, false, "状态错误")
				return
			}

			ex["state"] = state
		}

		if maintained > 0 {
			if maintained > 2 {
				helper.RetData(ctx, false, "状态错误")
				return
			}

			ex["maintained"] = maintained
		}

		if gameType > 0 {
			if gameType > 10 {
				helper.RetData(ctx, false, helper.GameTypeErr)
				return
			}

			ex["game_type"] = gameType
		}
	}
	if platform_type == 1 {
		ex["id"] = g.Op{"lt": 3000}
	}
	if platform_type == 2 {
		ex["id"] = g.Op{"gte": 3000}
	}
	data, err := model.PlatformList(ex, page, pageSize)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, data)
}

type platformUpdate struct {
	ID         string `json:"id"`
	State      int    `json:"state"`
	Maintained int    `json:"maintained"`
	Seq        int    `json:"seq"`
}

// Update 场馆更新
func (that *PlatformController) Update(ctx *fasthttp.RequestCtx) {

	param := platformUpdate{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	//fmt.Println(param)
	admin := session.AdminGet(ctx)

	err = model.PlatformUpdate(admin["id"], admin["name"], param.ID, param.State, param.Maintained, param.Seq)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}
